package test7;

import java.util.Arrays;

public class Demo4 {
    public static void main(String[] args) {
        int[] nums = {3, 2, 5, 6, 1, 2, 7};
//        quickSort(nums, 0, nums.length - 1);
        bubbleSort(nums);
        System.out.println(Arrays.toString(nums));
    }

    static void quickSort(int[] nums, int l, int r) {
        if (l > r) return;
        int i = l, j = r;
        int t = nums[l];
        while (i != j) {
            while (i < j && t < nums[j]) j--;
            if (i < j) nums[i++] = nums[j];

            while (i < j && t > nums[i]) i++;
            if (i < j) nums[j--] = nums[i];
        }

        nums[i] = t;
        quickSort(nums, l, i - 1);
        quickSort(nums, i + 1, r);
    }

    public static void bubbleSort(int[] nums) {
        for (int i = 0; i < nums.length; i++) {
            int flag = 0;
            for (int j = nums.length - 1; j > i; j--) {
                if (nums[j] < nums[j - 1]) {
                    flag = 1;
                    int temp = nums[j];
                    nums[j] = nums[j - 1];
                    nums[j - 1] = temp;
                }
            }
            if (flag == 0) return;
        }
    }


}
